package com.qidian.mes.qc.service.impl;

import com.qidian.mes.qc.service.DemoService;
import com.qidian.mes.util.pojo.MESResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Calendar;


@Service
public class DemoServiceImpl implements DemoService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public MESResult gen() {
        String indexCode = "XXXID";
        StringBuilder id = new StringBuilder();
        id.append("I0");

        // 从redis获取序列
        Long code = stringRedisTemplate.opsForValue().increment(indexCode);
        String idStr = code.toString();
        if (idStr.length() < 5) {
            // 前面加0
            for (int i = 0; i < 5 - idStr.length(); i++) {
                idStr = "0" + idStr;
            }
        }
        id.append(idStr);
        return MESResult.ok(id);

    }

    @Override
    public MESResult template() {
        String indexCode = "XXXID";
        StringBuilder id = new StringBuilder();
        id.append("QCT");
           // 时间
        Calendar calendar = Calendar.getInstance();
        id.append(calendar.get(Calendar.YEAR) );

        // 从redis获取序列
        Long code = stringRedisTemplate.opsForValue().increment(indexCode);
        String idStr = code.toString();
        if (idStr.length() < 4) {
            // 前面加0
            for (int i = 0; i < 4 - idStr.length(); i++) {
                idStr = "0" + idStr;
            }
        }
        id.append(idStr);
        return MESResult.ok(id);

    }

    @Override
    public MESResult iqc() {
        String indexCode = "XXXID";
        StringBuilder id = new StringBuilder();
        id.append("IQC");
        // 时间
        Calendar calendar = Calendar.getInstance();
        id.append(calendar.get(Calendar.YEAR) +(calendar.get(Calendar.MONTH) + 1) +
                calendar.get(Calendar.DATE));

        // 从redis获取序列
        Long code = stringRedisTemplate.opsForValue().increment(indexCode);
        String idStr = code.toString();
        if (idStr.length() < 4) {
            // 前面加0
            for (int i = 0; i < 4 - idStr.length(); i++) {
                idStr = "0" + idStr;
            }
        }
        id.append(idStr);
        return MESResult.ok(id);
    }
}


















